<?php

class PostController extends Controller {
    public function actionList() {
        $criteria = new CDbCriteria;
        $criteria->alias = 'p';
        $criteria->distinct = true;
        $criteria->join = 'left join post_detail pd on (p.type is null and p.id = pd.post_id) '.
                'left join post_image pi on p.id = pi.post_id';
        
        $criteria->addCondition(array('pd.language = :language','p.type is null'));
        $criteria->params = array(':language' => Yii::app()->language);
        
        // count result to display multipages
        $count = Post::model()->count($criteria);
        $pages = new CPagination($count);
        // results per page
        $pages->pageSize = 10;
        $pages->applyLimit($criteria);
        
        $results = Post::model()->findAll($criteria);
        $posts = array();
        if (count($results) > 0) {
            foreach ($results as $p) {
                $title;
                $content;
                if (count($p->postDetails) > 0) {
                    $title = $p->postDetail()->title;
                    $content = $p->postDetail()->content;
                }
                $imgUrl = '';
                if (count($p->postImages) > 0) {
                    foreach ($p->postImages as $image) {
                        $imgUrl = Yii::app()->getBaseUrl(true).'/'.$image->path;
                    }
                }
                $excerpt = Util::getExcerpt($content, 90);
                $posts[] = array('id' => $p->id,
                        'title' => $title,
                        'content' => $excerpt['excerpt'],
                        'hasMore' => $excerpt['hasMore'],
                        'image' => $imgUrl);
            }
        }

        // create breadcrumbs
        $this->breadcrumbs[] = Yii::t('default', 'News');
        $this->pageTitle = Yii::t('default', 'News');

        // render 'list' view
        $this->render('list', array('posts' => $posts, 'pages' => $pages));
    }

    public function actionView() {
        $id = Yii::app()->request->getQuery('id');
        if($id == null) {
            throw new CHttpException(404, Yii::t('default', 'notExistWeb'));
        }
        
        $post = Post::model()->findByPk($id);
        if ($post == null) {
            throw new CHttpException(404, Yii::t('default', 'notExistWeb'));
        }
        
        $data = array();
        $data['post'] = $post;
        
        $postDetail = $post->getPostDetail(Yii::app()->language);
        if($postDetail != null) {
            $data['postDetail'] = $postDetail;
        }
        $postImages = $post->postImages;
        if($postImages != null && count($postImages) > 0) {
            $data['postImages'] = $postImages;
        }
        
        // create breadcrumbs and pageTitle
        $this->pageTitle = '';
        
        $this->breadcrumbs[Yii::t('default', 'News')] = $this->createUrl('post/list');
        $this->breadcrumbs[] = $postDetail->title;
        $this->pageTitle .= $postDetail->title;
        
        // render view page
        $this->render('view', $data);
    }

}
